package com.horoscope.astrology.zodiac.signs.free.utils.ad

import android.app.Activity
import com.horoscope.astrology.zodiac.signs.free.BuildConfig
import com.horoscope.astrology.zodiac.signs.free.bean.ConfigResultBean
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADInterstitialManager
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADNativeManager
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADRewardManager
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_implement.LoadFacebookAd
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_implement.LoadIronSourceAd
import com.ironsource.mediationsdk.IronSource
import com.ironsource.mediationsdk.integration.IntegrationHelper
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 *  广告初始化
 *  @param (MobileAds.initialize)
 * */
class ADConfig {

    companion object {
        const val FB = "Facebook"
        const val ADMOB = "Admob"
        const val IS = "IronSource"
        const val UNITY = "Unity"
        const val FALLBACK_USER_ID = "f1d33eaf-4bb2-47b6-8d79-de2daf85b837"
        val mInstance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            ADConfig()
        }
    }

    private var mActivity: Activity? = null

    // 根据接口传过来的数据对几个类型的广告做初始化
    fun init(bean: ConfigResultBean.ConfigResultBeanData, activity: Activity) {
        // AdMob 初始化
//        MobileAds.initialize(MmoApplication.getInstance(), bean.ADConfig.Admob.AppID)
        mActivity = activity

        /** IronSource ads 初始化 **/
        initIronSourceAds(activity, bean)

        /** Unity ads 初始化 **/
//        initUnityAds(activity)

        /** 初始化所有类型广告 **/
        initConfig(bean)
    }

    private fun initConfig(bean: ConfigResultBean.ConfigResultBeanData) {
        // ADID 5 2 2 每次请求的数量
        Observable.create(ObservableOnSubscribe<Int?> { emitter ->
            emitter.onNext(1)
            emitter.onNext(2)
            emitter.onNext(3)
            emitter.onNext(4)
            emitter.onComplete()
        }).subscribeOn(Schedulers.io()) //在io执行上述操作
            .observeOn(AndroidSchedulers.mainThread()) //在UI线程执行下面操作
            .subscribe(object : Observer<Int?> {
                override fun onSubscribe(d: Disposable) {}

                override fun onNext(t: Int) {
                    when (t) {
                        1 -> {
                            ADInterstitialManager.mInstance.initConfig(bean)
                        }
                        2 -> {
                            ADNativeManager.mInstance.initConfig(bean)
                        }
                        3 -> {
                            ADRewardManager.mInstance.initConfig(mActivity!!, bean)
                        }
                        4 -> {
                            LoadFacebookAd.mInstance.loadNativeManager()
                        }
                    }

                }

                override fun onError(e: Throwable) {}

                override fun onComplete() {}
            })
    }

    private fun initIronSourceAds(activity: Activity, bean: ConfigResultBean.ConfigResultBeanData) {
        // 确保在初始化SDK之前设置了侦听器，以免丢失任何信息
        LoadIronSourceAd.mInstance.initListener()
        IronSource.setConsent(true)
        // 获取广告客户ID应该在后台线程上完成
        if (BuildConfig.DEBUG) {
            // 用于验证集成。 上线之前请先删除
            IntegrationHelper.validateIntegration(activity)
            IronSource.init(activity, "85460dcd")
            IronSource.shouldTrackNetworkState(activity, true)
        } else {
            IronSource.init(activity, bean.ADConfig.IronSource.AppID)
            IronSource.shouldTrackNetworkState(activity, true)
        }
    }

//    private fun initUnityAds(activity: Activity) {
//        UnityAds.initialize(activity, "123456", BuildConfig.DEBUG)
//    }
}